<?php if( ! defined('BASEPATH')) exit('No direct script access allowed');
/********************
 *@param function:OplusCms 後臺 操作日誌
 *@param datetime 2012-01-12
 *@param author solgh
 ********************/
class Operator extends Admins_Controller {
  
  private $data=array();

  public function __construct(){
    parent::__construct();
    $this->data['currentPage']='systems';
    $this->load->model('operator_mdl');
    $this->data['format_time']=$this->settings['default_time'];
	$this->data['lang'] =$this->lang->language;
  }

  public function index(){
    $this->get_list();
    //$this->output->enable_profiler(true);
    $this->load->view('admins/operator_list_view',$this->data);
  }
  
  //獲取列表
  private function get_list(){
    $page = $this->input->get('p',TRUE);
    $page = (!empty($page) && is_numeric($page)) ? intval($page) : 1;
    $limit = $this->settings['default_admin_limit'];
    //$limit =1;
    $offset = ($page - 1) * $limit;
    if($offset < 0){
      redirect('admins/operator');
    }
    $url_query=$_SERVER['QUERY_STRING'];
    if(!empty($url_query)){
      $url_query=preg_replace('/&p=(\d+)/','',$url_query);
    }
    $this->data['operator']=$this->input->get('operator');
    $this->data['title']=$this->input->get('title');
    $this->data['action']=$this->input->get('action');
    
    $this->data['field']=$this->input->get('field');
    $this->data['sort']=$this->input->get('sort');
    $this->data['field']=!empty($this->data['field'])?$this->data['field']:'add_time';
    $this->data['sort']=!empty($this->data['sort'])?$this->data['sort']:'desc';
    
    $data=array('language_id'=>$this->settings['default_language'],'limits'=>$limit,'offset'=>$offset);
    $data=!empty($this->data['operator'])?array_merge($data,array('operator'=>trim($this->data['operator']))):$data;
    $data=!empty($this->data['title'])?array_merge($data,array('title'=>trim($this->data['title']))):$data;  
    $data=!empty($this->data['action'])?array_merge($data,array('action'=>trim($this->data['action']))):$data;  
    
    $data=!empty($this->data['field'])?array_merge($data,array('field'=>$this->data['field'])):$data;
    $data=!empty($this->data['sort'])?array_merge($data,array('sort'=>strtoupper($this->data['sort']))):$data;

    $this->data['lists_count']=$this->operator_mdl->totle_operator($data);
    
    $pagination = '';
    if($this->data['lists_count'] > $limit)
    {
      $this->dpagination->currentPage($page);
      $this->dpagination->items($this->data['lists_count']);
      $this->dpagination->limit($limit);
      $this->dpagination->adjacents(5);
      $this->dpagination->target(site_url('admins/operator').'?'.$url_query);
      $this->dpagination->parameterName('p');
      $this->dpagination->nextLabel($this->data['lang']['orders_lable_19']);
      $this->dpagination->PrevLabel($this->data['lang']['orders_lable_18']);
      $pagination = $this->dpagination->getOutput();
    }
    $this->data['pagination'] = $pagination;
    $this->data['lists']=$this->operator_mdl->get_operator($data);
  }
    
  //刪除諮詢
  public function delete($delete_id=0){
    if(!empty($delete_id)){
      $result=$this->operator_mdl->delete_operator($delete_id);
      $msg = $result > 0?$this->data['lang']['global_lable_48']:$this->data['lang']['global_lable_49'];
      $notify = $result > 0?'success':'error';
      $this->nsession->set_flashdata($notify, $msg);
      redirect("admins/operator");
    }else{
      show_error('對不起,參數出錯!');
      exit;
    }
  }
   //批量刪除
  public function delete_batch(){
     $delete_string=$this->input->get('delete_string');
     if(!empty($delete_string)){
        $del_arr=explode(',',$delete_string);
        $result=$this->operator_mdl->delete_batch($del_arr);
        $msg = $result > 0?$this->data['lang']['global_lable_48']:$this->data['lang']['global_lable_49'];
        $notify = $result > 0?'success':'error';
        $this->nsession->set_flashdata($notify, $msg);
        redirect("admins/operator");
      }else{
        show_error('對不起,參數出錯');
        exit;
      }
  }
  
  //清空日誌
  public function delete_all(){
    $result=$this->operator_mdl->empty_table();
    $msg = $result > 0?$this->data['lang']['global_lable_48']:$this->data['lang']['global_lable_49'];
    $notify = $result > 0?'success':'error';
    $this->nsession->set_flashdata($notify, $msg);
    redirect("admins/operator");
  }
}